package com.example.checkall;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private CheckBox check_all;
	private ListView lView;
	private CheckAdapter checkAdapter;
	private Button reverse;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		check_all = (CheckBox) findViewById(R.id.check_all);
		lView = (ListView) findViewById(R.id.list_view);
		reverse = (Button) findViewById(R.id.reverse);

		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 15; i++) {
			list.add("这是条目" + i);
		}

		checkAdapter = new CheckAdapter(getApplicationContext(), list);
		lView.setAdapter(checkAdapter);

		check_all.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				boolean flag = check_all.isChecked();

				for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
					checkAdapter.getSelect().set(i, flag);
				}
				checkAdapter.notifyDataSetChanged();
			}
		});

		reverse.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				if (!checkAdapter.getSelect().contains(true)) {
					//check_all.setChecked(false);
					Toast.makeText(getApplicationContext(), "请选择0", 0).show();
				}else {
					for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
						if (checkAdapter.getSelect().get(i)) {
							checkAdapter.getSelect().set(i, false);
						}else {
							checkAdapter.getSelect().set(i, true);
						}
					}
					if (!checkAdapter.getSelect().contains(true)) {
						check_all.setChecked(false);
					}
				}
				checkAdapter.notifyDataSetChanged();

			}
		});

	}

	private class CheckAdapter extends BaseAdapter {
		private List<String> list;
		private Context context;
		private LinkedList<Boolean> linkedList = new LinkedList<Boolean>();

		public CheckAdapter(Context context, List<String> list) {
			// TODO Auto-generated constructor stub
			this.list = list;
			this.context = context;
			// 初始化
			for (int i = 0; i < list.size(); i++) {
				getSelect().add(false);
			}
		}

		private List<Boolean> getSelect() {
			return linkedList;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(context, R.layout.item, null);
			}
			CheckBox ck = (CheckBox) convertView.findViewById(R.id.ck);
			TextView tView = (TextView) convertView.findViewById(R.id.tv);

			tView.setText(list.get(position));

			ck.setChecked(linkedList.get(position));
			// 不能用onCheckChangedListner()复用的时候
			ck.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					linkedList.set(position, !linkedList.get(position));
					if (linkedList.contains(false)) {
						check_all.setChecked(false);
					} else {
						check_all.setChecked(true);
					}
					// 刷新
					notifyDataSetChanged();
				}
			});
			return convertView;
		}

	}

}
